﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace V5_EaspLibs.FrameWork.Model
{
    public class ModelPlugin
    {
        #region 访问器
        private string _Status;
        private string _Name;
        private string _DirName;
        private string _ModuleName;
        private string _Assembly;
        private List<Querys> _ListQuery = new List<Querys>();
        private string _Version;
        private string _Desc;
        private string _IsStall;
        private string _AdminUrlName;
        private string _AdminUrlType;
        private List<AdminUrls> _ListAdminUrls = new List<AdminUrls>();
        private List<ViewUrls> _ListViewUrls = new List<ViewUrls>();
        private InStall _VInStall = new InStall();
        private UnStall _VUnStall = new UnStall();
        /// <summary>
        /// 插件状态
        /// </summary>
        public string Status
        {
            get { return _Status; }
            set { _Status = value; }
        }
        /// <summary>
        /// 模块名称
        /// </summary>
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        /// <summary>
        /// 插件目录
        /// </summary>
        public string DirName
        {
            get { return _DirName; }
            set { _DirName = value; }
        }
        /// <summary>
        /// 模块英文名称
        /// </summary>
        public string ModuleName
        {
            get { return _ModuleName; }
            set { _ModuleName = value; }
        }
        /// <summary>
        /// 程序集
        /// </summary>
        public string Assembly
        {
            get { return _Assembly; }
            set { _Assembly = value; }
        }
        /// <summary>
        /// 模块查询
        /// </summary>
        public List<Querys> ListQuery
        {
            get { return _ListQuery; }
            set { _ListQuery = value; }
        }
        /// <summary>
        /// 版本号
        /// </summary>
        public string Version
        {
            get { return _Version; }
            set { _Version = value; }
        }
        /// <summary>
        /// 说明
        /// </summary>
        public string Desc
        {
            get { return _Desc; }
            set { _Desc = value; }
        }
        /// <summary>
        /// 插件是否安装
        /// </summary>
        public string IsStall
        {
            get { return _IsStall; }
            set { _IsStall = value; }
        }
        /// <summary>
        /// 后台菜单显示的名字
        /// </summary>
        public string AdminUrlName
        {
            get { return _AdminUrlName; }
            set { _AdminUrlName = value; }
        }
        /// <summary>
        /// 后台菜单显示的类型
        /// </summary>
        public string AdminUrlType
        {
            get { return _AdminUrlType; }
            set { _AdminUrlType = value; }
        }
        /// <summary>
        /// 后台系统的菜单Url
        /// </summary>
        public List<AdminUrls> ListAdminUrls
        {
            get { return _ListAdminUrls; }
            set { _ListAdminUrls = value; }
        }
        /// <summary>
        /// 前台重写的菜单和生成静态的Url配置
        /// </summary>
        public List<ViewUrls> ListViewUrls
        {
            get { return _ListViewUrls; }
            set { _ListViewUrls = value; }
        }


        /// <summary>
        /// 安装
        /// </summary>
        public InStall VInStall
        {
            get { return _VInStall; }
            set { _VInStall = value; }
        }
        /// <summary>
        /// 卸载
        /// </summary>
        public UnStall VUnStall
        {
            get { return _VUnStall; }
            set { _VUnStall = value; }
        }
        #endregion

        /// <summary>
        /// 载入所有的插件配置
        /// </summary>
        /// <returns></returns>
        public static List<ModelPlugin> ListLoadAllPlugin()
        {
            List<ModelPlugin> ListPluginHelper = new List<ModelPlugin>();
            try
            {
                DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "/Admin/Plugin/");
                //获取目录信息
                DirectoryInfo[] directorylist = di.GetDirectories();
                string path = string.Empty;
                foreach (DirectoryInfo dii in directorylist)
                {
                    ModelPlugin modelPluginHelper = new ModelPlugin();
                    path = AppDomain.CurrentDomain.BaseDirectory + "/Admin/Plugin/" + dii.Name + "/Plugin.config";
                    if (!File.Exists(path))
                        continue;
                    XmlDocument xml = new XmlDocument();
                    xml.Load(path);
                    XmlNode nodeName = xml.SelectSingleNode("plugin/status");
                    if (nodeName != null)
                        modelPluginHelper.Status = nodeName.InnerText;
                    nodeName = xml.SelectSingleNode("plugin/name");
                    if (nodeName != null)
                        modelPluginHelper.Name = nodeName.InnerText;
                    modelPluginHelper.DirName = dii.Name;
                    nodeName = xml.SelectSingleNode("plugin/modulename");
                    if (nodeName != null)
                        modelPluginHelper.ModuleName = nodeName.InnerText;
                    nodeName = xml.SelectSingleNode("plugin/assembly");
                    if (nodeName != null)
                        modelPluginHelper.Assembly = nodeName.InnerText;
                    nodeName = xml.SelectSingleNode("plugin/version");
                    if (nodeName != null)
                        modelPluginHelper.Version = nodeName.InnerText;
                    nodeName = xml.SelectSingleNode("plugin/desc");
                    if (nodeName != null)
                        modelPluginHelper.Desc = nodeName.InnerText;
                    //
                    nodeName = xml.SelectSingleNode("plugin/isstall");
                    if (nodeName != null)
                        modelPluginHelper.IsStall = nodeName.InnerText;
                    //
                    XmlNodeList NodesList = xml.SelectNodes("plugin/iquery/query");
                    if (NodesList != null)
                    {
                        foreach (XmlNode node in NodesList)
                        {
                            Querys modelQuery = new Querys();
                            modelQuery.Name = node.Attributes["name"].Value;
                            modelQuery.ClassNameSpace = node.Attributes["classnamespace"].Value;
                            modelPluginHelper.ListQuery.Add(modelQuery);
                        }
                    }
                    //
                    nodeName = xml.SelectSingleNode("plugin/adminurls");
                    if (nodeName != null)
                    {
                        modelPluginHelper.AdminUrlName = nodeName.Attributes["name"].Value;
                        modelPluginHelper.AdminUrlType = nodeName.Attributes["type"].Value;
                    }
                    NodesList = xml.SelectNodes("plugin/adminurls/url");
                    if (NodesList != null)
                    {
                        foreach (XmlNode node in NodesList)
                        {
                            AdminUrls modelAdminUrls = new AdminUrls();
                            modelAdminUrls.Name = node.Attributes["name"].Value;
                            modelAdminUrls.Href = node.Attributes["href"].Value;
                            modelAdminUrls.Target = node.Attributes["target"].Value;
                            modelPluginHelper.ListAdminUrls.Add(modelAdminUrls);
                        }
                    }
                    //
                    NodesList = xml.SelectNodes("plugin/viewurls/url");
                    if (NodesList != null)
                    {
                        foreach (XmlNode node in NodesList)
                        {
                            ViewUrls modelViewUrls = new ViewUrls();
                            modelViewUrls.PluginSelf = modelPluginHelper.ModuleName;
                            modelViewUrls.Type = node.SelectSingleNode("type").InnerText;
                            modelViewUrls.Title = node.SelectSingleNode("title").InnerText;
                            modelViewUrls.FileName = node.SelectSingleNode("filename").InnerText;
                            modelViewUrls.Lookfor = node.SelectSingleNode("lookfor").InnerText;
                            modelViewUrls.SendUrl = node.SelectSingleNode("sendurl").InnerText;
                            modelViewUrls.RewriteUrl = node.SelectSingleNode("rewriteurl").InnerText;
                            modelViewUrls.HtmlUrl = node.SelectSingleNode("htmlurl").InnerText;
                            modelPluginHelper.ListViewUrls.Add(modelViewUrls);
                        }
                    }
                    #region
                    NodesList = xml.SelectNodes("plugin/install/sqls/sql");
                    if (NodesList != null)
                    {
                        foreach (XmlNode node in NodesList)
                        {
                            ModelPlugin.SQLS sql = new SQLS();
                            sql.SQL = node.InnerText;
                            modelPluginHelper.VInStall.LSQLS.Add(sql);
                        }
                    }
                    NodesList = xml.SelectNodes("plugin/install/files/file");
                    if (NodesList != null)
                    {
                        foreach (XmlNode node in NodesList)
                        {
                            ModelPlugin.Files file = new Files();
                            file.File = node.InnerText;
                            modelPluginHelper.VInStall.LFiles.Add(file);
                        }
                    }
                    #endregion
                    #region
                    NodesList = xml.SelectNodes("plugin/unstall/sqls/sql");
                    if (NodesList != null)
                    {
                        foreach (XmlNode node in NodesList)
                        {
                            ModelPlugin.SQLS sql = new SQLS();
                            sql.SQL = node.InnerText;
                            modelPluginHelper.VInStall.LSQLS.Add(sql);
                        }
                    }
                    NodesList = xml.SelectNodes("plugin/unstall/files/file");
                    if (NodesList != null)
                    {
                        foreach (XmlNode node in NodesList)
                        {
                            ModelPlugin.Files file = new Files();
                            file.File = node.InnerText;
                            modelPluginHelper.VInStall.LFiles.Add(file);
                        }
                    }
                    #endregion
                    ListPluginHelper.Add(modelPluginHelper);
                }
            }
            catch
            {
            }
            return ListPluginHelper;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <returns></returns>
        public static ModelPlugin GetPluginItemModel(string xmlPath)
        {
            ModelPlugin modelPluginHelper = new ModelPlugin();
            string path = AppDomain.CurrentDomain.BaseDirectory + "/Admin/Plugin/" + xmlPath + "/Plugin.config";
            if (!File.Exists(path))
                return null;
            XmlDocument xml = new XmlDocument();
            xml.Load(path);
            XmlNode nodeName = xml.SelectSingleNode("plugin/status");
            if (nodeName != null)
                modelPluginHelper.Status = nodeName.InnerText;
            nodeName = xml.SelectSingleNode("plugin/name");
            if (nodeName != null)
                modelPluginHelper.Name = nodeName.InnerText;
            modelPluginHelper.DirName = xmlPath;
            nodeName = xml.SelectSingleNode("plugin/modulename");
            if (nodeName != null)
                modelPluginHelper.ModuleName = nodeName.InnerText;
            nodeName = xml.SelectSingleNode("plugin/assembly");
            if (nodeName != null)
                modelPluginHelper.Assembly = nodeName.InnerText;
            nodeName = xml.SelectSingleNode("plugin/version");
            if (nodeName != null)
                modelPluginHelper.Version = nodeName.InnerText;
            nodeName = xml.SelectSingleNode("plugin/desc");
            if (nodeName != null)
                modelPluginHelper.Desc = nodeName.InnerText;
            //
            nodeName = xml.SelectSingleNode("plugin/isstall");
            if (nodeName != null)
                modelPluginHelper.IsStall = nodeName.InnerText;
            //
            XmlNodeList NodesList = xml.SelectNodes("plugin/iquery/query");
            if (NodesList != null)
            {
                foreach (XmlNode node in NodesList)
                {
                    Querys modelQuery = new Querys();
                    modelQuery.Name = node.Attributes["name"].Value;
                    modelQuery.ClassNameSpace = node.Attributes["classnamespace"].Value;
                    modelPluginHelper.ListQuery.Add(modelQuery);
                }
            }
            //
            nodeName = xml.SelectSingleNode("plugin/adminurls");
            if (nodeName != null)
            {
                modelPluginHelper.AdminUrlName = nodeName.Attributes["name"].Value;
                modelPluginHelper.AdminUrlType = nodeName.Attributes["type"].Value;
            }
            NodesList = xml.SelectNodes("plugin/adminurls/url");
            if (NodesList != null)
            {
                foreach (XmlNode node in NodesList)
                {
                    AdminUrls modelAdminUrls = new AdminUrls();
                    modelAdminUrls.Name = node.Attributes["name"].Value;
                    modelAdminUrls.Href = node.Attributes["href"].Value;
                    modelAdminUrls.Target = node.Attributes["target"].Value;
                    modelPluginHelper.ListAdminUrls.Add(modelAdminUrls);
                }
            }
            //
            NodesList = xml.SelectNodes("plugin/viewurls/url");
            if (NodesList != null)
            {
                foreach (XmlNode node in NodesList)
                {
                    ViewUrls modelViewUrls = new ViewUrls();
                    modelViewUrls.PluginSelf = modelPluginHelper.ModuleName;
                    modelViewUrls.Type = node.SelectSingleNode("type").InnerText;
                    modelViewUrls.Title = node.SelectSingleNode("title").InnerText;
                    modelViewUrls.FileName = node.SelectSingleNode("filename").InnerText;
                    modelViewUrls.Lookfor = node.SelectSingleNode("lookfor").InnerText;
                    modelViewUrls.SendUrl = node.SelectSingleNode("sendurl").InnerText;
                    modelViewUrls.RewriteUrl = node.SelectSingleNode("rewriteurl").InnerText;
                    modelViewUrls.HtmlUrl = node.SelectSingleNode("htmlurl").InnerText;
                    modelPluginHelper.ListViewUrls.Add(modelViewUrls);
                }
            }
            #region
            NodesList = xml.SelectNodes("plugin/install/sqls/sql");
            if (NodesList != null)
            {
                foreach (XmlNode node in NodesList)
                {
                    ModelPlugin.SQLS sql = new SQLS();
                    sql.SQL = node.InnerText;
                    modelPluginHelper.VInStall.LSQLS.Add(sql);
                }
            }
            NodesList = xml.SelectNodes("plugin/install/files/file");
            if (NodesList != null)
            {
                foreach (XmlNode node in NodesList)
                {
                    ModelPlugin.Files file = new Files();
                    file.File = node.InnerText;
                    modelPluginHelper.VInStall.LFiles.Add(file);
                }
            }
            #endregion
            #region
            NodesList = xml.SelectNodes("plugin/unstall/sqls/sql");
            if (NodesList != null)
            {
                foreach (XmlNode node in NodesList)
                {
                    ModelPlugin.SQLS sql = new SQLS();
                    sql.SQL = node.InnerText;
                    modelPluginHelper.VInStall.LSQLS.Add(sql);
                }
            }
            NodesList = xml.SelectNodes("plugin/unstall/files/file");
            if (NodesList != null)
            {
                foreach (XmlNode node in NodesList)
                {
                    ModelPlugin.Files file = new Files();
                    file.File = node.InnerText;
                    modelPluginHelper.VInStall.LFiles.Add(file);
                }
            }
            #endregion
            return modelPluginHelper;
        }


        #region 插件配置文件所用到的类库
        public class Querys
        {
            public string Name { get; set; }
            public string ClassNameSpace { get; set; }
        }
        /// <summary>
        /// 后台Url集合
        /// </summary>
        public class AdminUrls
        {
            public string Name { set; get; }
            public string Href { set; get; }
            public string Target { set; get; }
        }
        /// <summary>
        /// 前台Url集合
        /// </summary>
        public class ViewUrls
        {
            public string PluginSelf { get; set; }
            public string Type { get; set; }
            public string Title { get; set; }
            public string FileName { get; set; }
            public string Lookfor { get; set; }
            public string SendUrl { get; set; }
            public string RewriteUrl { get; set; }
            public string HtmlUrl { get; set; }
        }
        #endregion


        /// <summary>
        /// 安装
        /// </summary>
        public class InStall
        {
            private List<SQLS> _LSQLS = new List<SQLS>();
            private List<Files> _LFiles = new List<Files>();
            /// <summary>
            /// 安装SQL
            /// </summary>

            public List<SQLS> LSQLS
            {
                get { return _LSQLS; }
                set { _LSQLS = value; }
            }
            /// <summary>
            /// 安装文件
            /// </summary>
            ///             
            public List<Files> LFiles
            {
                get { return _LFiles; }
                set { _LFiles = value; }
            }
        }
        /// <summary>
        /// 卸载
        /// </summary>
        public class UnStall
        {
            private List<SQLS> _LSQLS = new List<SQLS>();
            private List<Files> _LFiles = new List<Files>();
            /// <summary>
            /// 卸载SQL
            /// </summary>

            public List<SQLS> LSQLS
            {
                get { return _LSQLS; }
                set { _LSQLS = value; }
            }
            /// <summary>
            /// 卸载文件
            /// </summary>
            ///             
            public List<Files> LFiles
            {
                get { return _LFiles; }
                set { _LFiles = value; }
            }
        }
        /// <summary>
        /// SQL
        /// </summary>
        public class SQLS
        {
            public string SQL { get; set; }
        }
        /// <summary>
        /// 文件
        /// </summary>
        public class Files
        {
            public string File { get; set; }
        }
        /// <summary>
        /// 获取所有前台Url集合
        /// </summary>
        /// <returns></returns>
        public static List<ViewUrls> GetAllViewUrls()
        {
            List<ModelPlugin.ViewUrls> list = new List<ModelPlugin.ViewUrls>();
            List<ModelPlugin> listPluginHelper = ModelPlugin.ListLoadAllPlugin();
            foreach (ModelPlugin p in listPluginHelper)
            {
                foreach (ModelPlugin.ViewUrls item in p.ListViewUrls)
                {
                    list.Add(item);
                }
            }
            return list;
        }
    }
}
